package com.example.audiorecord.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.audiorecord.R;
import com.example.audiorecord.mediaplayer.MediaPlayerHolder;
import com.example.audiorecord.mediaplayer.PlaybackInfoListener;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 录音文件播放页面
 */
public class PlayerActivity extends AppCompatActivity implements PlaybackInfoListener,SeekBar.OnSeekBarChangeListener{
    private ImageButton back;
    private TextView name;
    private File file;
    private SeekBar seekBar;
    private MediaPlayerHolder mediaPlayerHolder = new MediaPlayerHolder();
    private Timer timer = new Timer();
    private Chronometer chronometer;
    private Button playAndPauseBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        initView();
        initData();
        initMediaPlayer();
    }

    private void initMediaPlayer() {
        //初始化mediaPlayer
        mediaPlayerHolder.loadMedia(file.getAbsolutePath());
        mediaPlayerHolder.setmPlaybackInfoListener(this);
        seekBar.setOnSeekBarChangeListener(this);

    }

    private void initView() {
        back = findViewById(R.id.back_btn);
        name = findViewById(R.id.file_name);
        seekBar = findViewById(R.id.seek_bar);
        chronometer = findViewById(R.id.chro);
        playAndPauseBtn = findViewById(R.id.pause_and_play_btn);

        /**
         * 暂停开始按钮的点击事件
         */
        playAndPauseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayerHolder.isPlaying()){
                    playAndPauseBtn.setBackgroundResource(R.drawable.start_btn_icon);
                    mediaPlayerHolder.pause();
                    chronometer.stop();
                }else{
                    playAndPauseBtn.setBackgroundResource(R.drawable.pause_btn_icon);
                    mediaPlayerHolder.play();
                    chronometer.start();
                }
            }
        });
    }

    private void initData() {
        //获取从上一个页面传过来的录音文件
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        file = (File) extras.getSerializable("FILE");
        name.setText(file.getName());
    }

    public void backOnClick(View view) {
        //点击返回按钮销毁页面
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //页面销毁释放mediaplayer，停止计时器
        mediaPlayerHolder.release();
        timer.cancel();
        chronometer.stop();
    }

    /**
     * mediaplayer的回调事件
     * @param duration
     */
    @Override
    public void onDurationChanged(int duration) {
        seekBar.setMax(mediaPlayerHolder.duration());
        chronometer.setBase(SystemClock.elapsedRealtime());
    }

    @Override
    public void onPositionChanged(int position) {

    }

    @Override
    public void onStateChanged(int state) {
        if(state == MediaPlayerHolder.PLAYSTATUS4){
            //媒体流装载完成调用播放
            mediaPlayerHolder.play();
            timer.schedule(timerTask,0,200);
            chronometer.start();
        }else if(state == MediaPlayerHolder.PLAYSTATUSD1){
            //媒体流装载错误
        }
    }

    @Override
    public void onPlaybackCompleted() {
        chronometer.stop();
        playAndPauseBtn.setBackgroundResource(R.drawable.start_btn_icon);
    }

    /**
     * seekbar监听
     * @param seekBar
     * @param i
     * @param b
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        chronometer.setBase(SystemClock.elapsedRealtime() - seekBar.getProgress());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        //拖到进度条设置当前播放进度
        int progress = seekBar.getProgress();
        mediaPlayerHolder.seekTo(progress);
    }

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            //获取当前音乐播放的位置
            int currentPosition = mediaPlayerHolder.currentDuration();
            //让进度条动起来
            seekBar.setProgress(currentPosition);
        }
    };

}